﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Kimd.Vision
{
    [Serializable]
    [XmlInclude(typeof(Rectangle2NB))]
    [XmlInclude(typeof(CircleNB))]
    [XmlInclude(typeof(LineNB))]
    [XmlInclude(typeof(PointNB))]
    [XmlInclude(typeof(PolygonNB))]
    [XmlInclude(typeof(Rectangle1NB))]
    [XmlInclude(typeof(SectorNB))]
    [XmlInclude(typeof(TextNB))]
    public class ShapeNB : ICloneable
    {
        #region Property
        public ShapeType TypeName { get; set; }
        protected int distance2Handle = 20;
        /// <summary>
		/// 是否处于编辑状态
		/// </summary>
        [XmlIgnore]
		public bool IsSelected { get; set; }
        /// <summary>
        /// 具体的拖拽或改变大小的类型
        /// </summary>
        [XmlIgnore]
        public PositionNB ChangeModel { get; set; }
        public string PaintColor { get; set; }
        /// <summary>
        /// 图形颜色
        /// </summary>
        public Color PaintColor2 { get; set; }
        /// <summary>
        /// 图形边宽
        /// </summary>
        public int LineWidth { get; set; }
        /// <summary>
        /// 是否填充
        /// </summary>
        public bool IsFill { get; set; }
        /// <summary>
        /// 填充色
        /// </summary>
        public Color FillColor { get; set; }
        /// <summary>
        /// 对区域合并或相减的描述
        /// </summary>

        public ShapeOperator RegOperator { get; set; }
        /// <summary>
        /// 形状别名
        /// </summary>
        public string Alias { get; set; }
        /// <summary>
        /// 
        /// </summary>

        [XmlIgnore]
        public bool IsShowOnly;
        /// <summary>
        /// 只使能平移与旋转
        /// </summary>

        [XmlIgnore]
        public bool IsMoveOnly;
        /// <summary>
        /// Shape改变时触发
        /// </summary>

        [XmlIgnore]
        public Action<PositionNB, ShapeNB> NotifyShapeChange;
        #endregion

        #region Constructor
        public ShapeNB()
        {
            this.PaintColor = "green";
            this.LineWidth = 1;
		}
        #endregion

        #region Method
        /// <summary>
        /// 绘图方法
        /// </summary>
        /// <param name="g">窗口绘图graphics</param>
        /// <param name="pixel2Window">图片点转窗口点方法</param>
        /// <param name="lenConvert">图片长度转窗口长度方法</param>
        public virtual void Draw(HWindow window, Graphics g, Func<PointF, PointF> pixel2Window, Func<float, float> lenConvert, bool disableEdit)
        {

        }
        /// <summary>
        /// 点与shape的关系
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public virtual PositionNB PointOnShape(PointF point, Func<float, float> lenConvert)
        {
            return PositionNB.Out;
        }
        /// <summary>
        /// 图形被拖拽或改变尺寸时的操作
        /// </summary>
        /// <param name="position"></param>
        public virtual void Change(PointF point)
        {
			
        }
        public virtual HObject ToRegion()
        {
            return null;
        }
        public virtual PointF GetCenter()
        {
            return new PointF();
        }
        public void SetOperator(bool isAdd)
        {
            RegOperator = isAdd ? ShapeOperator.Union : ShapeOperator.Difference;
        }
        public float DistancePp(PointF testPoint, PointF stationaryPoint)
        {
            return (float)(Math.Sqrt(Math.Pow(testPoint.X - stationaryPoint.X, 2) + Math.Pow(testPoint.Y - stationaryPoint.Y, 2)));
        }
		public void SetAlias(string name)
		{
			this.Alias = name;
		}
		public object Clone()
        {
            return MemberwiseClone();
        }
        public virtual void FromXmlNode(XmlNode node)
        {

        }
        public virtual void ToXmlNode(out Dictionary<string, object> attributes, out Dictionary<string, object> nodes)
        {
            attributes = new Dictionary<string, object>();
            nodes = new Dictionary<string, object>();
        }
        #endregion
    }

    #region enum
    public enum ShapeOperator
    {
        Union,
        Difference
    }
    public enum ShapeType
    {
        Point,
        Line,
        Circle,
        Rectangle1,
        Rectangle2,
        Polygon,
        Sector
    }
    public enum ShapeMode
    {
        Display,
        Edit
    }
    public enum PositionNB
    {
        Out, //不在边缘也不在中心
        In, //在中心
        LeftUp,
        RightUp,
        RightDown,
        LeftDown,
        Up,
        Down,
        Left,
        Right,
        Angle,
        StartAngle,
        EndAngle,
        Create,
		OuterArc,
		InnerArc
	}
    #endregion
}
